package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.user.client.entity.AppAccount;
import com.meida.module.user.client.entity.AppUser;
import com.meida.module.user.provider.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 后台App用户列表
 */
@Component
public class AppAccountPageListHandler implements PageInterceptor<AppAccount> {

    @Autowired
    private AppUserService appUserService;

    @Override
    public void prepare(CriteriaQuery<AppAccount> cq, PageParams pageParams, EntityMap params) {
    }

    @Override
    public void complete(CriteriaQuery<AppAccount> cq, List<EntityMap> result, EntityMap extra) {
        result.forEach(entityMap -> {
            Long userId = entityMap.getLong("userId");
            if (FlymeUtils.isNotEmpty(userId)) {
                AppUser appUser = appUserService.getById(userId);
                String areaCode = appUser.getAreaCode();
                entityMap.put("areaCode", areaCode);
            }
        });
    }
}
